﻿using System;
using System.Collections.Generic;
using System.Text;
using CSRedis;
using MQ.Core.Util;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;

namespace MQ.Core.Cache
{
    public class RedisCache
    {
        private readonly static CSRedisClient csredis;
        static RedisCache()
        {
            csredis = new CSRedisClient(RedisOption.Uri);
        }
        #region 连接信息

        #endregion
        private readonly TimeSpan timeSpan = new TimeSpan(0, 30, 0);//默认30分钟
        #region String
        public static void Remove(string key)
        {
            csredis.Del(key);
        }

        public static T Get<T>(string key)
        {
            return csredis.Get<T>(key);
        }

        public static void Set<T>(string key, T value)
        {
            csredis.Set(key, value);
        }

        public static void Set<T>(string key, T value, int expire)
        {
            csredis.Set(key, value, expire);
        }
        public static void Remove(string[] keys)
        {
            csredis.Del(keys);
        }
        public static bool SetNx<T>(string key,T value)
        {
            return csredis.SetNx(key, value);
        }
        public static Task<bool> SetNxAsync<T>(string key, T value)
        {
            return csredis.SetNxAsync(key, value);
        }
        #endregion

        #region hash
        public static bool HSet(string key,string field, object value)
        {
            return csredis.HSet(key, field, value);
        }

        public static Task<bool> HSetAsync(string key, string field, object value)
        {
            return csredis.HSetAsync(key, field, value);
        }

        public static bool HSetNx(string key, string field, object value)
        {
            return csredis.HSetNx(key, field, value);
        }

        public static Task<bool> HSetNxAsync(string key, string field, object value)
        {
            return csredis.HSetNxAsync(key, field, value);
        }

        public static T HGet<T>(string key) where T : class, new()
        {
            var data = csredis.HGetAll(key);
            return data.ToObject<T>();
        }

        public static T HGet<T>(string key,string filed)
        {
            return csredis.HGet<T>(key, filed);
        }
        public static Task<T> HGetAsync<T>(string key, string filed)
        {
            return csredis.HGetAsync<T>(key, filed);
        }
        #endregion

        #region set

        #endregion

        #region list
        public static long LPushX(string key,object value)
        {
            return csredis.LPushX(key, value);
        }
        public static Task<long> LPushXAsync(string key, object value)
        {
            return csredis.LPushXAsync(key, value);
        }
        public static long LPush<T>(string key, params T[] value)
        {
            return csredis.LPush(key, value);
        }
        public static Task<long> LPushAsync<T>(string key, params T[] value)
        {
            return csredis.LPushAsync(key, value);
        }
        public static T LPop<T>(string key)
        {
            return csredis.LPop<T>(key);
        }
        public static Task<T> LPopAsync<T>(string key)
        {
            return csredis.LPopAsync<T>(key);
        }
        public static T[] LRange<T>(string key,long start,long stop)
        {
            return csredis.LRange<T>(key, start, stop);
        }
        public static Task<T[]> LRangeAsync<T>(string key, long start, long stop)
        {
            return csredis.LRangeAsync<T>(key, start, stop);
        }
        public static T LIndex<T>(string key,long index)
        {
            return csredis.LIndex<T>(key, index);
        }
        public static Task<T> LIndexAsync<T>(string key, long index)
        {
            return csredis.LIndexAsync<T>(key, index);
        }
        #endregion

        #region sorted list

        #endregion
        public static void Clear()
        {
            csredis.Del();
        }

        public static void Clear(int dbId)
        {
            throw new NotImplementedException();
        }

        

        #region 发布订阅
        public long Publish(string channel,string msg)
        {
            return csredis.Publish(channel,msg);
        }
        public Task<long> PublishAsync(string channel, string msg)
        {
            return csredis.PublishAsync(channel, msg);
        }
        /// <summary>
        /// 根据channel名称来订阅
        /// </summary>
        /// <param name="channels"></param>
        public void Subscribe(params(string, Action<CSRedisClient.SubscribeMessageEventArgs> action)[] channels)
        {
            csredis.Subscribe(channels);
        }
        /// <summary>
        /// 模糊订阅
        /// 模糊订阅，订阅所有分区节点(同条消息只处理一次），返回SubscribeObject，PSubscribe(new [] { "chan1*", "chan2*"
    //     }, msg => Console.WriteLine(msg.Body))
        /// </summary>
        /// <param name="channelPatterns"></param>
        /// <param name="pmessage"></param>
        public void PSubscribe(string[] channelPatterns, Action<CSRedisClient.PSubscribePMessageEventArgs> pmessage)
        {
            csredis.PSubscribe(channelPatterns, pmessage);
        }
        #endregion
    }
}
